<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CategoryAdminController
 *
 * @author dro
 */

namespace Gairal\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Gairal\CoreBundle\Form\CategoryType;
use Gairal\CoreBundle\Entity\Category;

class CategoryAdminController extends Controller {

    public function indexAction() {
        //Call addAction to save a new category il request is POST
        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
            $this->addAction($request);

        $listCategories = $this->getDoctrine()
                ->getManager()
                ->getRepository('GairalCoreBundle:Category')
                ->findAllWithSkillsAndInterests();
        $maxOrder = count($listCategories) > 0 ? $listCategories[count($listCategories) - 1]->getDisplayOrder() : 0;

        return $this->render('GairalAdminBundle:Category:index.html.twig', array(
                    'action' => 'Admin - Categories',
                    'listCategories' => $listCategories,
                    'maxOrder' => $maxOrder
        ));
    }

    public function addAction($request = null) {

        $category = new Category();
        $form = $this->createForm(new CategoryType(), $category);

        if ($request == null) {
            return $this->render('GairalAdminBundle:Category:add.html.twig', array(
                        'form' => $form->createView()
            ));
        }
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {
                $maxOrder = $this->getDoctrine()
                        ->getManager()
                        ->getRepository('GairalCoreBundle:Category')
                        ->findMaxOrder();
                $category->setDisplayOrder(++$maxOrder);

                $em = $this->getDoctrine()->getManager();
                $em->persist($category);
                $em->flush();

                $this->get('session')->getFlashBag()->add('info', 'Category "' . $category->getName() . '" added');
                return $this->redirect($this->generateUrl('AdminCategory'));
            }else
                $this->get('session')->getFlashBag()->add('error', $form->getErrorsAsString());
        }
    }

    public function removeAction(Category $category) {

        if ($category != null) {
            $this->getDoctrine()
                    ->getManager()
                    ->getRepository('GairalCoreBundle:Category')
                    ->removeAndReorder($category);
            $this->get('session')->getFlashBag()->add('info', 'Category "' . $category->getName() . '" removed');
        }
        else
            $this->get('session')->getFlashBag()->add('error', 'Category not removed, error in url');

        return $this->redirect($this->generateUrl('AdminCategory'));
    }

    public function modifyAction(Category $category) {
        $form = $this->createForm(new CategoryType(), $category);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {

                $em = $this->getDoctrine()->getManager();
                $em->persist($category);
                $em->flush();

                $this->get('session')->getFlashBag()->add('info', 'Category "' . $category->getName() . '" modified');
                return $this->redirect($this->generateUrl('AdminCategory'));
            }
            else
                $this->get('session')->getFlashBag()->add('error', $form->getErrorsAsString());
        }
        return $this->render('GairalAdminBundle:Category:modify.html.twig', array(
                    'action' => 'Admin - Category - modify - '. $category->getName(),
                    'form' => $form->createView(),
                    'category' => $category
        ));
    }

    public function upAction(Category $category) {
        $this->getDoctrine()
                ->getManager()
                ->getRepository('GairalCoreBundle:Category')
                ->swapWith($category, $category->getDisplayOrder() + 1);

        $this->get('session')->getFlashBag()->add('info', 'Category "' . $category->getName() . '" down\'d');
        
        return $this->redirect($this->generateUrl('AdminCategory'));
    }

    public function downAction(Category $category) {
        $this->getDoctrine()
                ->getManager()
                ->getRepository('GairalCoreBundle:Category')
                ->swapWith($category, $category->getDisplayOrder() - 1);

        $this->get('session')->getFlashBag()->add('info', 'Category "' . $category->getName() . '" up\'d');

        return $this->redirect($this->generateUrl('AdminCategory'));
    }

}

?>
